import time
from selenium import webdriver
from selenium.common.exceptions import InvalidArgumentException, WebDriverException, NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class Base:
    def __init__(self,browser):
        # 打开浏览器，设置driver
        if browser == 'chrome':
            self.driver = webdriver.Chrome()
        elif browser == 'firefox':
            self.driver = webdriver.Firefox()
        elif browser == 'edge':
            self.driver == webdriver.Edge()
        else:
            print('不支持浏览器')
            self.driver = None

    def get(self,url):
        # 请求目标网址
        try:
            self.driver.get(url)
        except (InvalidArgumentException, WebDriverException):
            print('输入的地址不正确')

    def find_element(self,locator):
        # 定位元素
        try:
            return self.driver.find_element(*locator)
        except NoSuchElementException:
            print('元素定位失败')

    def click(self,locator):
        # 元素点击操作
        element = self.find_element(locator)
        if element is not None:
            element.click()
        else:
            print('元素定位失败')

    def get_attribute(self,value):
        # 获取元素属性 作业
        ellipsis = self.find_element(locator)
        if ellipsis is not None:
            print(ellipsis.get_attribute(value))
        else:
            print('没有获取到属性')
    def advance(self):
        self.driver.advance()

    def back(self):
        self.driver.back()

    def refresh(self):
        self.driver.refresh

    def quit(self,seconds=0):
        # 退出浏览器
        time.sleep(seconds)
        self.driver.quit()

if __name__ == '__main__':
    base = Base('chrome')
    base.get('https://www.baidu.com')
    base.retreat()